package com.py.credit.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import com.py.credit.dao.PromiseBookReportDao;
import com.py.credit.entity.PromiseBookLog;
import com.py.credit.entity.PromiseBookReport;
import com.py.credit.service.PromiseBookReportService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;

@Service
public class PromiseBookReportServiceImpl implements PromiseBookReportService{
	
	@Autowired
	PromiseBookReportDao promiseBookReportDao;

	@Override
	public List<Map<String,Object>> getPromiseBooksByConditionForPage(String type,String ztType,String bookStatus,String havBook,String sql ,
			List<String> showColumns, String[] additionalColumns, String condition,Page page,Long deptId,Long userId,String sbtype) {
		
		if (CollectionUtils.isEmpty(showColumns)) {
            throw new ServiceException("至少选择一列");
        }
        List<String> conditions = SQLBuilder.createConditionSql(condition);
        
        List<Map<String, Object>> list = promiseBookReportDao.getPromiseBooksByConditionForPage(type,ztType,bookStatus, havBook ,sql ,showColumns,additionalColumns, conditions, page,deptId,userId,sbtype);
		
		return list;
	}

	@Override
	public int savePromiseBook(String absolutepath,String type,String ztName,String tyshxydm,MultipartFile file,Long officeId,String officeName , Long id) {
		// 获取文件的后缀
        String fileType = null;
        
      //承诺书名称
		
        String newName = "";
        String name = "";
		if (file != null) {
			name = file.getOriginalFilename();
			
            // 获取文件后缀
            if (file.getOriginalFilename().contains(".")) {
            	
                fileType = name.substring(name.lastIndexOf("."),name.length());
                newName = name.substring(0,name.lastIndexOf("."));
            }
            
            if (!".jpg".equalsIgnoreCase(fileType) && !".png".equalsIgnoreCase(fileType)) {
            	throw new ServiceException("只能上传jpg/png格式的文件！");
            }
            
            //文件夹路径
            String dirPath = absolutepath + File.separator + "upload" + File.separator + officeId + File.separator + "promise-book-import" + File.separator + (new SimpleDateFormat ("yyyy-MM-dd hh-mm-ss-SSS").format(new Date()));
            File dir = new File(dirPath);
            if(!dir.exists()) {
            	dir.mkdirs();
            }
            
            //承诺书路径
    		String path = dirPath + File.separator + file.getOriginalFilename();
    		
    		
    		
    		//上传至服务器
    		try {
                file.transferTo(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
    		
    		if(!StringUtils.isEmpty(String.valueOf(id))) {
    			return promiseBookReportDao.updatePromiseBook(newName,id,type,ztName,path,tyshxydm,"1");
    		}
            
        } else {
        	return promiseBookReportDao.updatePromiseBook(newName,id,type,ztName,"",tyshxydm,"");
        }
		
		return 0;
		
	}
	
	@Override
	public int delPromiseBooks(List<Long> ids) {
		return promiseBookReportDao.delPromiseBooks(ids);
	}

	@Override
	public int savePromiseBooks(List<PromiseBookReport> pbrList) {
		return promiseBookReportDao.savePromiseBooks(pbrList);
	}
	
	@Override
	public String promiseBookPreview(Long id) {
		return promiseBookReportDao.promiseBookPreview(id);
	}

	@Override
	public int approvalMyself(List<Long> idList) {
		return promiseBookReportDao.approvalMyself(idList);
	}
	
	@Override
	public Map<String,Object> selectOne(Long id){
		return promiseBookReportDao.selectOne(id);
	}
	
	@Override
	public void insertErrorLog(PromiseBookLog promiseBookLog){
		promiseBookReportDao.insertErrorLog(promiseBookLog);
	}
	
	@Override
	public List<Map<String,Object>> selectAllLogs(Page page){
		return promiseBookReportDao.selectAllLogsForPage(page);
	}
	
	@Override
	public String getDownloadLogPath(String id) {
		return promiseBookReportDao.getDownloadLogPath(id);
	}

	@Override
	public Map<String, String> selectCount(Long id) {
		return promiseBookReportDao.selectCount(id);
	}

	@Override
	public String selectAllParentIds(Long deptId) {
		return promiseBookReportDao.selectAllParentIds(deptId);
	}
	

}
